package com.fc.common.log;


import org.springframework.data.redis.core.RedisTemplate;
import com.fc.common.spring.SpringContextHolder;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.AppenderBase;

/** 
 * @ClassName: RedisLoggingAppender 
 * @Description: TODO
 * @author wanghw 
 * @date 2016年5月25日 
 */
public class RedisLoggingAppender extends AppenderBase<LoggingEvent>{
	
	private LoggingHolder holder = LoggingHolder.instance();
	
	private RedisTemplate<String, Object> logTemplate;
	
	private synchronized void initRedisTemplate(){
		if(logTemplate == null){
			try{
				logTemplate = SpringContextHolder.getBean("logTemplate");
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	
	@Override
	protected void append(LoggingEvent event){
		holder.append(event, new LoggingCallBack(){
			@Override
			public void log(String log) {
				if(logTemplate == null){
					initRedisTemplate();
				}
				logTemplate.opsForList().rightPush(LoggingConfig.redismq, log);
			}
			
		});
	}
}
